import os
from flask import Flask
from classify_checker import ClassifyChecker
from dotenv import load_dotenv
from typing import List
from checker.base_checker import BaseChecker
from checker.cosmetics_checker import CosmeticsChecker
from checker.commodity_checker import CommodityChecker
from checker.secondmedical_checker import SecondMedicalChecker
from checker.cosmetics_checker import CosmeticsWithPicsChecker
from checker.officesupplies_checker import OfficesuppliesChecker
from checker.clothes_checker import ClothesChecker
from checker.digital_checker import DigitalChecker
from flask_sqlalchemy import SQLAlchemy
from dotenv import load_dotenv

load_dotenv()

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('SQLALCHEMY_DATABASE_URI')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True

db = SQLAlchemy(app)
cosmeticsChecker = CosmeticsChecker()
cosmeticsWithPicChecker = CosmeticsWithPicsChecker()
commodityChecker = CommodityChecker()
secondMedicalChecker = SecondMedicalChecker()
digitalChecker = DigitalChecker()
officesuppliesChecker = OfficesuppliesChecker()
clothesChecker = ClothesChecker()
cc = ClassifyChecker()

def getChecker(classify_name:str, pics:List[str]) -> BaseChecker:
    if classify_name == "化妆品":
        if len(pics) > 0:
            return cosmeticsWithPicChecker
        else:
            return cosmeticsChecker
    elif classify_name == "办公文具":
        return officesuppliesChecker
    elif classify_name == "数码产品":
        return digitalChecker
    elif classify_name == "服装鞋帽":
        return clothesChecker
    elif classify_name == "二级医疗器械":
        return secondMedicalChecker
    else:
        return None



def check_product_name(product_name: str, pics: List[str]) -> dict:
    # 进行合规性检查
    classify_name = cc.check_product_classify(product_name)
    print("classify name is ", classify_name.classify_name)
    cn = classify_name.classify_name
    checker = getChecker(cn, pics)
    if checker  == None:
        print("未知分类")
        return jsonify({
            'error': '未知分类',
            'message': '请联系管理员添加该分类'
        }), 400
    check_result = checker.check_product(product_name, pics)
    # 获取风险摘要
    risk_summary = checker.get_risk_summary(check_result)
    # 构建详细响应
    response = checker.buildResponse(product_name, pics, cn, check_result, risk_summary)
    return response


if __name__ == '__main__':
    app.run(debug=True)


